//
//  robRedPacketViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/9/22.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "robRedPacketViewController.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "CMessageMgr.h"
#import "CMessageModel.h"
#import "CXmppRequestMgr.h"
#import "UIUtil.h"
#import "MBProgressHUD.h"
#import "redPacketResultViewController.h"
#import "RedPacketInfo.h"
@interface robRedPacketViewController ()
@property(nonatomic,strong)CContactModel *fromUser;
@property(nonatomic,strong)CMessageModel *message;
@property(nonatomic,strong)CContactModel *currentUser;
@property(nonatomic,weak)UIView *topView;
@end

@implementation robRedPacketViewController
-(CContactModel *)currentUser
{
    if (_currentUser == nil) {
        _currentUser = [[[CContactMgr alloc]init] loadCurContact];
    }
    return _currentUser;
}
-(void)setMessageID:(NSString *)MessageID
{
    _MessageID = MessageID;
    self.message = [[[CMessageMgr alloc]init] loadMessageByMessageId:MessageID];
    
    if (self.message.GroupId.length>0) {
        self.fromUser = [[[CContactMgr alloc]init] loadContactWithUserId:_message.MineUserId];
    }
    else
    {
        self.fromUser = [[[CContactMgr alloc]init] loadContactWithUserId:_message.FriendUserId];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(246, 75, 77);
    [self.navigationItem setNewTitleCenter:@"红包"];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"nav_fh_icon" selectedImageName:@"nav_fh_click_icon" target:self sel:@selector(popVc)];
    if (IsIOS7) {
        [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(246, 75, 77)];
    }
    else
    {
        [self.navigationController.navigationBar setTintColor:RGBCOLOR(246, 75, 77)];
    }

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configInterFace];
    
}
-(void)configInterFace
{
    UIView *top = [[UIView alloc]init];
    top.backgroundColor = [UIColor clearColor];
    self.topView = top;
    [self.view addSubview:top];
    [top mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(90);
    }];
    UIImageView *imageView = [[UIImageView alloc]init];
     [imageView sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:self.fromUser.UserID]] placeholderImage:[UIImage imageNamed:@"testHeader"]];
    imageView.layer.cornerRadius = 30;
    imageView.layer.masksToBounds = YES;
    [top addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(60);
        make.centerX.mas_equalTo(top);
    }];
    UIImageView *backImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"组-6"]];
    backImageView.userInteractionEnabled = YES;
    [self.view addSubview:backImageView];
    [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(0);
        make.left.right.bottom.mas_equalTo(0);
    }];
    UIImageView *hbImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hb"]];
    [backImageView addSubview:hbImageView];
    [hbImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(80);
        make.centerX.mas_equalTo(backImageView);
    }];
    UILabel *nameLable = [[UILabel alloc]init];
    nameLable.textColor = RGBCOLORWITH16COLOR(0x323232);
    nameLable.text = self.fromUser.UserName;
    nameLable.font = [UIFont systemFontOfSize:16];
    nameLable.textAlignment = NSTextAlignmentCenter;
    [backImageView addSubview:nameLable];
    [nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(hbImageView.mas_bottom).offset(20);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    UILabel *contentLable = [[UILabel alloc]init];
    contentLable.font = [UIFont systemFontOfSize:24];
    contentLable.textAlignment = NSTextAlignmentCenter;
    contentLable.text = @"发来一个红包";
    contentLable.textColor = RGBCOLORWITH16COLOR(0xc9787a);
    [backImageView addSubview:contentLable];
    [contentLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLable.mas_bottom).offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.font = [UIFont systemFontOfSize:13];
    timeLabel.textColor = RGBCOLORWITH16COLOR(0x8f8f8f);
    timeLabel.textAlignment = NSTextAlignmentCenter;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[_message.TimeStamp doubleValue] / 1000.0];
    NSString *str1 = [UIUtil getOverdueYMDHMSDateStringWithTimeStamp:date];
    NSString *str2 = [UIUtil timeForMessageStyle:date];
    timeLabel.text = [NSString stringWithFormat:@"%@ %@",str1,str2];
    [backImageView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentLable.mas_bottom).offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(30);
    }];
    UIButton *openbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [openbutton addTarget:self action:@selector(openPacket) forControlEvents:UIControlEventTouchUpInside];
    openbutton.backgroundColor = RGBCOLORWITH16COLOR(0xfdc82a);
    openbutton.titleLabel.textColor = [UIColor whiteColor];
    [openbutton setTitle:@"拆开红包" forState:UIControlStateNormal];
    openbutton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [backImageView addSubview:openbutton];
    [openbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLabel.mas_bottom).offset(15);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(40);
        make.centerX.mas_equalTo(backImageView);
    }];
}
-(void)openPacket
{
    __weak typeof(self) weakSelf = self;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"打开中...";
    [[CXmppRequestMgr shareInstance] getMoneyFromRedPacketWithUserId:self.currentUser.UserID useName:self.currentUser.UserName messageId:self.MessageID successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
        [hud hide:YES];
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
           __block redPacketResultViewController *result = [[redPacketResultViewController alloc]init];
            RedPacketInfo *info = [[RedPacketInfo alloc]init];
            NSDictionary *dic = [responseObject[@"redPacketInfo"] lastObject];
            info.money = [dic[@"amount"] doubleValue];
            info.userId = dic[@"userId"];
            info.userName = dic[@"userName"];
            info.time = dic[@"sendTime"];
            info.redPacketId = dic[@"redPacketId"];
            info.robed = [dic[@"robStatus"] boolValue];
            result.redinfo = info;
            result.isTimeOut = NO;
            result.message = self.message;
            dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.navigationController pushViewController:result animated:YES];
            });
        }
        else
        {
            __block redPacketResultViewController *result = [[redPacketResultViewController alloc]init];
            RedPacketInfo *info = [[RedPacketInfo alloc]init];
            info.money = 0;
            result.redinfo = info;
            result.isTimeOut = NO;
            result.message = self.message;
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.navigationController pushViewController:result animated:YES];
            });

        }
        
    } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showError:@"网络错误,请重试"];
    }];
}

- (void)popVc {
    
}

@end
